<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\SearchApi;
use App\Http\Controllers\Api\IndexApi;
use App\Model\Goods;
use App\Http\Controllers\Api\SearchCommon;
use App\Http\Controllers\Home\ShowLinksController;

class HomeSearch extends Controller
{

    /*
    * 处理搜索
    */
    public function DoSearch(Request $request)
    {

    	$sate = IndexApi::getsate();

    	//查询友情链接数据
        $link = new ShowLinksController;
        $linksArray = $link->index();

        //查询父级分类信息
        $index = new IndexApi();
        $types = $index->getTypes();

        //实例化搜索类
    	$search = new SearchApi('abc');

        //查询热搜词
    	$Hotsearch = $search->getHot();

    	//调用搜索方法获取索引
    	$res = $search->doSearch($request->keyword);


    	//遍历商品id 拿到商品图片
    	$ids = [];
    	foreach ($res as $k => $v) {

    		if(!empty(Goods::find($v['goods_id']))) {

    			$res[$k]['goods_pic'] = Goods::select('goods_pic')->where('id', $v['goods_id'])->first()->toArray();

    		}
    	}

    	//实力化搜索分页类
    	$page = SearchCommon::CustomPagination($request, $res ,8);


    	$count = count($res);

    	return view('Home.goods-search', ['res' => $page, 'keyword' => $request->keyword, 'sate' => $sate, 'linksArray' => $linksArray, 'types' => $types, 'HotSearch' => $Hotsearch, 'count' => $count]);
    }

}
